﻿using PoliAsis.Domain.Concrete;
using PoliAsis.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PoliAsis.Business
{
    public class Utils
    {
        public static ErrorStatus VerifyUser(string auth_token)
        {
            ErrorStatus st = new ErrorStatus();
  
            if (String.IsNullOrEmpty(auth_token))
            {
                st.err_message = StatusMessages.REQUIERD_DATA_MISSING;
                st.valid = false;
                return st;
            }

            Guid user_token;
            if (!Guid.TryParse(auth_token, out user_token))
            {
                st.err_message = StatusMessages.INVALID_TOKEN;
                st.valid = false;
                return st;
            }

            //check the token
            PoliUser where = new PoliUser();
            where.auth_token = user_token.ToString();

            PoliUserDS uds = new PoliUserDS();
            PoliUser user_db = uds.SelectOne(where);

            if (user_db == null)
            {
                st.err_message = StatusMessages.USER_DOES_NOT_EXISTS;
                st.valid = false;
                return st;
            }

            TimeSpan tspan = DateTime.Now - user_db.auth_token_date.Value;
            if (tspan.TotalDays > 1)
            {
                st.valid = false;
                st.err_message = StatusMessages.OLD_TOKEN;
                return st;
            }

            st.userid = user_db.id;
            return st;

        }

    }

    public class ErrorStatus
    {
        public bool valid { get; set; }
        public string err_message { get; set; }
        public int? userid { get; set; }

        public ErrorStatus() {
            valid = true;
            err_message = "OK";
            userid = -1;
        }
    }

}
